package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.store.dao.ChannelStoreBrandConfigDao;
import com.zbkj.crmeb.store.model.ChannelStoreBrandConfig;
import com.zbkj.crmeb.store.request.CateHotRequest;
import com.zbkj.crmeb.store.service.ChannelStoreBrandConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class ChannelStoreBrandConfigServiceImpl extends ServiceImpl<ChannelStoreBrandConfigDao, ChannelStoreBrandConfig> implements ChannelStoreBrandConfigService {

    @Override
    public CommonResult<Object> cateHot(CateHotRequest request, Integer merId) {
        ChannelStoreBrandConfig channelStoreBrandConfig = Optional.ofNullable(getOne(Wrappers.lambdaQuery(ChannelStoreBrandConfig.class).eq(ChannelStoreBrandConfig::getMerId, merId)
                .eq(ChannelStoreBrandConfig::getBrandId, request.getBrandId()).last("LIMIT 1"))).orElse(new ChannelStoreBrandConfig());
        //分类热门品牌
        String cateHotType = CollectionUtils.isNotEmpty(request.getTypeList()) ? StringUtils.join(request.getTypeList(), ",") : "";
        if(channelStoreBrandConfig.getId() == null){
            channelStoreBrandConfig.setBrandId(request.getBrandId());
            channelStoreBrandConfig.setMerId(merId);
            channelStoreBrandConfig.setCreateTime(new Date());
        }
        channelStoreBrandConfig.setCateHot(cateHotType)
                .setHotTime(new Date())
                .setUpdateTime(new Date());
        return saveOrUpdate(channelStoreBrandConfig) ? CommonResult.success() : CommonResult.failed();
    }
}
